﻿// -----------------------------------------------------------------------
// <copyright file="MauPhieuCanLamSangDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class MauPhieuCanLamSangDataAccess
    {
        public bool InsertMauPhieuCanLamSang(MauPhieuCanLamSangEntity obj)
        {
            try
            {
                string sql = "spMauPhieuCanLamSang_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaMauPhieu", DbType.String, obj.MaMauPhieu);
                db.AddInParameter(cmd, "TenMauPhieu", DbType.String, obj.TenMauPhieu);
                db.AddInParameter(cmd, "NoiDung", DbType.String, obj.NoiDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertMauPhieuCanLamSang", ex.Message); throw ex; }
        }

        public bool EditMauPhieuCanLamSang(MauPhieuCanLamSangEntity obj)
        {
            try
            {
                string sql = "spMauPhieuCanLamSang_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaMauPhieu", DbType.String, obj.MaMauPhieu);
                db.AddInParameter(cmd, "TenMauPhieu", DbType.String, obj.TenMauPhieu);
                db.AddInParameter(cmd, "NoiDung", DbType.String, obj.NoiDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateMauPhieuCanLamSang", ex.Message); throw ex; }
        }

        public bool DeleteMauPhieuCanLamSang(string maMauPhieu)
        {
            try
            {
                string sql = "spMauPhieuCanLamSang_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaMauPhieu", DbType.String, maMauPhieu);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteMauPhieuCanLamSang", ex.Message); throw ex; }
        }

        public MauPhieuCanLamSangEntity GetMauPhieuCanLamSang(string maMauPhieu)
        {
            try
            {
                MauPhieuCanLamSangEntity obj = null;
                string sql = "spMauPhieuCanLamSang_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaMauPhieu", DbType.String, maMauPhieu);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new MauPhieuCanLamSangEntity();
                        obj.MaMauPhieu = reader["MaMauPhieu"].ToString();
                        obj.TenMauPhieu = reader["TenMauPhieu"].ToString();
                        obj.NoiDung = reader["NoiDung"].ToString();
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetMauPhieuCanLamSang", ex.Message); throw ex; }
        }

        public List<MauPhieuCanLamSangEntity> GetMauPhieuCanLamSangList()
        {
            try
            {
                List<MauPhieuCanLamSangEntity> list = new List<MauPhieuCanLamSangEntity>();
                MauPhieuCanLamSangEntity obj = null;
                string sql = "spMauPhieuCanLamSang_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new MauPhieuCanLamSangEntity();
                        obj.MaMauPhieu = reader["MaMauPhieu"].ToString();
                        obj.TenMauPhieu = reader["TenMauPhieu"].ToString();
                        obj.NoiDung = reader["NoiDung"].ToString();
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetMauPhieuCanLamSangList", ex.Message); throw ex; }
        }

    }
}